package es.decloud.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;

/**
 * This class is used to represent an address with address, city, province and
 * postal-code information.
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@Embeddable
@Indexed
public class Direccion extends BaseObject implements Serializable {

	private static final long serialVersionUID = -1086372773745019647L;

	private String direccion;
	private String ciudad;
	private String provincia;
	private String pais;
	private String codigoPostal;

	@Column(length = 150)
	@Field
	public String getDireccion() {
		return direccion;
	}

	@Column(length = 50)
	@Field
	public String getCiudad() {
		return ciudad;
	}

	@Column(length = 100)
	@Field
	public String getProvincia() {
		return provincia;
	}

	@Column(length = 100)
	@Field
	public String getPais() {
		return pais;
	}

	@Column(name = "codigo_postal", length = 15)
	@Field(analyze = Analyze.NO)
	public String getCodigoPostal() {
		return codigoPostal;
	}

	public void setDireccion(String direccion) {
		this.direccion = direccion;
	}

	public void setCiudad(String ciudad) {
		this.ciudad = ciudad;
	}

	public void setPais(String pais) {
		this.pais = pais;
	}

	public void setCodigoPostal(String codigoPostal) {
		this.codigoPostal = codigoPostal;
	}

	public void setProvincia(String provincia) {
		this.provincia = provincia;
	}

	/**
	 * Overridden equals method for object comparison. Compares based on
	 * hashCode.
	 * 
	 * @param o
	 *            Object to compare
	 * @return true/false based on hashCode
	 */
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Direccion)) {
			return false;
		}

		final Direccion address1 = (Direccion) o;

		return this.hashCode() == address1.hashCode();
	}

	/**
	 * Overridden hashCode method - compares on address, city, province, country
	 * and postal code.
	 * 
	 * @return hashCode
	 */
	public int hashCode() {
		int result;
		result = (direccion != null ? direccion.hashCode() : 0);
		result = 29 * result + (ciudad != null ? ciudad.hashCode() : 0);
		result = 29 * result + (provincia != null ? provincia.hashCode() : 0);
		result = 29 * result + (pais != null ? pais.hashCode() : 0);
		result = 29 * result + (codigoPostal != null ? codigoPostal.hashCode() : 0);
		return result;
	}

	/**
	 * Returns a multi-line String with key=value pairs.
	 * 
	 * @return a String representation of this class.
	 */
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("country", this.pais)
				.append("address", this.direccion).append("province", this.provincia)
				.append("postalCode", this.codigoPostal).append("city", this.ciudad).toString();
	}
}
